package com.zb.service;

import com.zb.enums.SmsType;
import com.zb.exception.SMSException;
import com.zb.util.SmsSendUtil;
import com.zb.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 验证码服务
 * @Author WEN
 * @Date 2023/7/10 0010
 **/
@Service
public class VerificSMSService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static VerificSMSService verificSMSService;

    /**
     * 验证码有效期（单位：秒）
     */
    private static final long EXPIRE_TIME = 2 * 60L;

    @PostConstruct
    public void init() {
        verificSMSService = this;
        verificSMSService.redisTemplate = this.redisTemplate;
    }

    /**
     * 发送登录类验证码
     * @param phone 手机号
     * @throws SMSException
     */
    public static void sendLoginVerificCode(String phone) throws SMSException {
        String code = RandomUtil.random6();
        // 发送验证码
        SmsSendUtil.sendVerifiCode(phone, code);
        // 缓存验证码
        verificSMSService.redisTemplate.opsForValue().set(SmsType.LOGIN.getType() + phone, code, EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 校验验证码
     * @param phone
     * @param code
     * @throws SMSException
     */
    public static void checkLoginVerificCode(String phone, String code) throws SMSException {
        if (StringUtils.isBlank(code)) {
            throw new SMSException("请输入验证码！");
        }

        String o = verificSMSService.redisTemplate.opsForValue().get(SmsType.LOGIN.getType() + phone);
        if (StringUtils.isBlank(o)) {
            throw new SMSException("验证码已过期！");
        }
        if (!Objects.equals(o, code)) {
            throw new SMSException("验证码输入错误！");
        }
        // 校验通过后删除验证码
        verificSMSService.redisTemplate.delete(SmsType.LOGIN.getType() + phone);
    }

    /**
     * 发送修改类验证码
     * @param phone 手机号
     * @throws SMSException
     */
    public static void sendUpdateVerificCode(String phone) throws SMSException {
        String code = RandomUtil.random6();
        // 发送验证码
        SmsSendUtil.sendVerifiCode(phone, code);
        // 缓存验证码
        verificSMSService.redisTemplate.opsForValue().set(SmsType.UPDATE.getType() + phone, code, EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 校验验证码
     * @param phone
     * @param code
     * @throws SMSException
     */
    public static void checkUpdateVerificCode(String phone, String code) throws SMSException {
        if (StringUtils.isBlank(code)) {
            throw new SMSException("请输入验证码！");
        }
        String o = verificSMSService.redisTemplate.opsForValue().get(SmsType.UPDATE.getType() + phone);
        if (StringUtils.isBlank(o)) {
            throw new SMSException("验证码已过期！");
        }
        if (!Objects.equals(o, code)) {
            throw new SMSException("验证码输入错误！");
        }
        // 校验通过后删除验证码
        verificSMSService.redisTemplate.delete(SmsType.UPDATE.getType() + phone);
    }



}
